
package gruppe7.servlets;

import gruppe7.model.Model;
import gruppe7.util.Html;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LookUpPriceServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doGet(request, response);
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	Html html = new Html();
        PrintWriter out = response.getWriter();
        RequestDispatcher rd = request.getRequestDispatcher("main_links.jsp");

        out.println(html.setHeader("Lookup Ticket Price"));
        out.println("<body>");
        rd.include(request, response);

        
        out.println("<div id ='content'>");
        out.println("<div id='content-left'>");
        out.println("<h3><img src='images/tickets.png' /> Ticket Lookups</h3><br/>");

        if (request.getAttribute("same") != null) {
        	out.println("<p class='red'>Please pick a destination station that is different from the departure station</p>");         
        }
        
        dropDownLists(response);   
        
        out.println("</div></div>");
        out.println(html.setFooter());

    }

    private void dropDownLists(HttpServletResponse response) throws IOException {

    	PrintWriter out = response.getWriter();
    	java.util.List<String> stations = null;
    	
    	try {
    		stations = Model.getInstance().getStationList();
    	}
    	
    	catch (Exception e) {
    		throw new IOException(e);
    	}
    	
        out.println(""
                + "<p><table class='default'>"
                + "<form action='/gruppe7/ticket_price.do' method='post'>"
                + "<tr><td>Departure</td>"
                + "<td><select name='departure'>");

        for (String station : stations) {

            out.println("<option value='" + station + "'>" + station + "</option>");
        }

        out.println("</select></td></tr></br>"
                + "<tr><td>Destination</td>"
                + "<td><select name='destination'>");

        for (String station : stations) {
            out.println("<option value='" + station + "'>" + station + "</option>");
        }

        out.println("</select></td></tr></br>"
                + "<tr><td></td><td align='right'><input class='submit' type='submit' value='Inform me!'/></td></tr>"
                + "</form></table></p>");
    }
}
